package com.leezai.mqtt.config;

import cn.hutool.dfa.WordTree;

import java.util.Arrays;
import java.util.List;

public class DfaSensitive {
    public static void main(String[] args) {
        // 1. 创建词树
        WordTree tree = new WordTree();

        // 2. 添加敏感词
        List<String> sensitiveWords = Arrays.asList("毒品", "赌博", "枪支");
        tree.addWords(sensitiveWords);

        // 3. 待检测文本
        String text = "他涉嫌赌博和毒品交易，持有非法枪支。";

        // 4. 检测文本中是否包含敏感词
        boolean hasSensitive = tree.isMatch(text);
        System.out.println("是否包含敏感词: " + hasSensitive);

        // 5. 找出第一个匹配词
        String firstMatch = tree.match(text);
        System.out.println("第一个匹配的敏感词: " + firstMatch);

        // 6. 找出所有匹配词
        List<String> allMatches = tree.matchAll(text);
        System.out.println("所有匹配的敏感词: " + allMatches);

        // 7. 将敏感词替换为***
        String cleanText = text;
        for (String word : allMatches) {
            cleanText = cleanText.replaceAll(word, "***");
        }
        System.out.println("替换后的文本: " + cleanText);
    }

}
